{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from cryptography.fernet import Fernet\n",
    "\n",
    "# 生成密钥\n",
    "def generate_key():\n",
    "    return Fernet.generate_key()\n",
    "\n",
    "# 加密函数\n",
    "def encrypt_message(message, key):\n",
    "    f = Fernet(key)\n",
    "    encrypted_message = f.encrypt(message.encode())\n",
    "    return encrypted_message\n",
    "\n",
    "# 解密函数\n",
    "def decrypt_message(encrypted_message, key):\n",
    "    f = Fernet(key)\n",
    "    decrypted_message = f.decrypt(encrypted_message).decode()\n",
    "    return decrypted_message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "DuplicateSectionError",
     "evalue": "Section 'licensing' already exists",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mDuplicateSectionError\u001b[0m                     Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[9], line 6\u001b[0m\n\u001b[0;32m      3\u001b[0m config \u001b[39m=\u001b[39m ConfigParser()\n\u001b[0;32m      4\u001b[0m config\u001b[39m.\u001b[39mread(\u001b[39m'\u001b[39m\u001b[39mconfig.ini\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m----> 6\u001b[0m config\u001b[39m.\u001b[39;49madd_section(\u001b[39m'\u001b[39;49m\u001b[39mlicensing\u001b[39;49m\u001b[39m'\u001b[39;49m)\n",
      "File \u001b[1;32mc:\\Python311\\Lib\\configparser.py:1226\u001b[0m, in \u001b[0;36mConfigParser.add_section\u001b[1;34m(self, section)\u001b[0m\n\u001b[0;32m   1222\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Create a new section in the configuration.  Extends\u001b[39;00m\n\u001b[0;32m   1223\u001b[0m \u001b[39mRawConfigParser.add_section by validating if the section name is\u001b[39;00m\n\u001b[0;32m   1224\u001b[0m \u001b[39ma string.\"\"\"\u001b[39;00m\n\u001b[0;32m   1225\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_validate_value_types(section\u001b[39m=\u001b[39msection)\n\u001b[1;32m-> 1226\u001b[0m \u001b[39msuper\u001b[39;49m()\u001b[39m.\u001b[39;49madd_section(section)\n",
      "File \u001b[1;32mc:\\Python311\\Lib\\configparser.py:673\u001b[0m, in \u001b[0;36mRawConfigParser.add_section\u001b[1;34m(self, section)\u001b[0m\n\u001b[0;32m    670\u001b[0m     \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m'\u001b[39m\u001b[39mInvalid section name: \u001b[39m\u001b[39m%r\u001b[39;00m\u001b[39m'\u001b[39m \u001b[39m%\u001b[39m section)\n\u001b[0;32m    672\u001b[0m \u001b[39mif\u001b[39;00m section \u001b[39min\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_sections:\n\u001b[1;32m--> 673\u001b[0m     \u001b[39mraise\u001b[39;00m DuplicateSectionError(section)\n\u001b[0;32m    674\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_sections[section] \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_dict()\n\u001b[0;32m    675\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_proxies[section] \u001b[39m=\u001b[39m SectionProxy(\u001b[39mself\u001b[39m, section)\n",
      "\u001b[1;31mDuplicateSectionError\u001b[0m: Section 'licensing' already exists"
     ]
    }
   ],
   "source": [
    "from configparser import ConfigParser\n",
    "\n",
    "config = ConfigParser()\n",
    "config.read('config.ini')\n",
    "\n",
    "config.add_section('licensing')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 重置running_counter的数值\n",
    "key = b'jVJZA3zs-9zGPwLzHyVJVhybqiS0CKvaTHl4aB3omn8='\n",
    "num = 10  # 限制次数\n",
    "\n",
    "# 设置配置文件中running_counter的数值\n",
    "config.set('licensing', 'running_counter', encrypt_message(bin(num), key).decode('utf-8'))\n",
    "config.set('licensing', 'start_day', encrypt_message(bin(date.today().toordinal()), key).decode('utf-8'))\n",
    "# 更新配置文件\n",
    "with open(\"config.ini\", 'w') as configfile:\n",
    "    config.write(configfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "739089\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    encrypt_counter = config.get('licensing', 'running_counter').encode('utf-8')\n",
    "    encrypt_date = config.get('licensing', 'start_day').encode('utf-8')\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "running_counter = int(decrypt_message(encrypt_counter, key),2)  # 运行次数\n",
    "start_day = int(decrypt_message(encrypt_date, key), 2)\n",
    "print(running_counter)\n",
    "print(start_day)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "739089\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "current_datetime = datetime.date.today().toordinal()\n",
    "print(current_datetime)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
